﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UM_SERVER.ProcessModel;
using UM_SERVER.Packet.InPacket;
using UM_SERVER.Net;

namespace UM_SERVER.ProcessModel
{
    class FriendProcessModel : Model
    {
        /// <summary>
        ///     对于要获取申请好友信息不存在的回馈
        /// </summary>
        /// <param name="inpackage">获取要添加用户的信息请求包</param>
        public static void ACK_CanNotFindPostFriend(GetPostFriendInfoPacket inpackage)
        {
            PackageBody = "USERNAME=\"" + inpackage.GetUsername() + "\" FRIENDUSERNAME=\"" + inpackage.GetFriendName() + "\"";
            PackageBody += " SESSIONCODE=\"" + inpackage.GetSessionCode() + "\"";
            //得到协议
            byte[] protocol1 = ProtocolFactory.ProtocolFactory.GetProtocol("ReplyGetPostFriendInfo",
                                                                          (int)
                                                                          Packet.Protocol.OUT_NetworkCommand.REPLY_GET_POST_FRIEND_INFO_NOT_FIND_PACKAGE,
                                                                          ((int)
                                                                           Packet.Protocol.NetworkProtocol.Base).
                                                                              ToString(),
                                                                          PackageBody,
                                                                          ProtocolFactory.ProtocolFactory.ACK.No,
                                                                          ProtocolFactory.ProtocolFactory.ENCRYPT.No);
            NetControl.Send(protocol1, inpackage.GetTalkPort(), inpackage.GetUserIpAddress());
        }

        /// <summary>
        ///     回复要获取的好友信息
        /// </summary>
        /// <param name="inpackage">获取要添加用户的信息请求包</param>
        /// <param name="DIC_USERINFO">好友信息字典</param>
        public static void ACK_GetPostFriendInfo(GetPostFriendInfoPacket inpackage, Dictionary<String, String> DIC_FRIENDINFO)
        {
            int TotalSize = 0;
            if (inpackage != null && DIC_FRIENDINFO != null)
            {
                foreach (KeyValuePair<string, string> pair in DIC_FRIENDINFO)
                {
                    if (pair.Key != "PASSWORD")
                    {
                        if (pair.Key != "SEX" && pair.Key != "AGE" && pair.Key != "USERID" && pair.Key != "MESSION" && pair.Key != "LOGINLIVE" && pair.Key != "TALKPORT")
                        {
                            TotalSize += 4 + Encoding.Default.GetBytes(pair.Value).Length;
                        }
                        else
                        {
                            TotalSize += 4;
                        }
                    }
                }
                byte[] Usernames = Encoding.Default.GetBytes(inpackage.GetUsername());
                byte[] UsernamesLength = BitConverter.GetBytes(Usernames.Length);
                byte[] packageBody = new byte[TotalSize + 4 + Usernames.Length];
                int Offset = 0;
                foreach (KeyValuePair<string, string> pair in DIC_FRIENDINFO)
                {
                    if (pair.Key != "PASSWORD")
                    {
                        if (pair.Key != "SEX" && pair.Key != "AGE" && pair.Key != "USERID" && pair.Key != "MESSION" && pair.Key != "LOGINLIVE" && pair.Key != "TALKPORT")
                        {
                            byte[] IntLegnth = BitConverter.GetBytes(Encoding.Default.GetBytes(pair.Value).Length);
                            byte[] Value = Encoding.Default.GetBytes(pair.Value);
                            Array.ConstrainedCopy(IntLegnth, 0, packageBody, Offset, 4);
                            Offset += 4;
                            Array.ConstrainedCopy(Value, 0, packageBody, Offset, Value.Length);
                            Offset += Value.Length;
                        }
                        else
                        {
                            byte[] IntLegnth = BitConverter.GetBytes(int.Parse(pair.Value));
                            Array.ConstrainedCopy(IntLegnth, 0, packageBody, Offset, 4);
                            Offset += 4;
                        }
                    }
                }
                Array.ConstrainedCopy(UsernamesLength, 0, packageBody, Offset, 4);
                Offset += 4;
                Array.ConstrainedCopy(Usernames, 0, packageBody, Offset, Usernames.Length);
                Offset += Usernames.Length;
                //得到协议
                byte[] protocol1 = ProtocolFactory.ProtocolFactory.GetProtocol("ReplyGetPostFriendInfo",
                                                                              (int)Packet.Protocol.OUT_NetworkCommand.REPLY_GET_POST_FRIEND_INFO,
                                                                              (int)Packet.Protocol.NetworkProtocol.Base,
                                                                              packageBody,
                                                                              ProtocolFactory.ProtocolFactory.ACK.No,
                                                                              ProtocolFactory.ProtocolFactory.ENCRYPT.No, "");
                NetControl.Send(protocol1, inpackage.GetTalkPort(), inpackage.GetUserIpAddress());
            }
        }

        /// <summary>
        ///     转发用户申请好友请求
        /// </summary>
        /// <param name="inpackage">申请好友请求包</param>
        /// <param name="user">被转发用户</param>
        public static void Redirect_PostAddFriend(AddFriendPacket inpackage, User.User user)
        {
            byte[] Username = Encoding.Default.GetBytes(inpackage.GetUsername());
            byte[] UsernameLength = BitConverter.GetBytes(Username.Length);
            byte[] ItemId = BitConverter.GetBytes(int.Parse(inpackage.GetItemId()));
            byte[] friend = Encoding.Default.GetBytes(inpackage.GetFriendName());
            byte[] friendLength = BitConverter.GetBytes(friend.Length);
            byte[] Session = Encoding.Default.GetBytes(inpackage.GetSessionCode());
            byte[] SessionLength = BitConverter.GetBytes(Session.Length);
            byte[] packagebody = new byte[Username.Length + friend.Length + Session.Length + 16];
            int Offset = 0;
            //复制用户名长度
            Array.ConstrainedCopy(UsernameLength, 0, packagebody, Offset, 4);
            Offset += 4;
            //复制用户名
            Array.ConstrainedCopy(Username, 0, packagebody, Offset, Username.Length);
            Offset += Username.Length;
            //复制分组ID
            Array.ConstrainedCopy(ItemId, 0, packagebody, Offset, 4);
            Offset += 4;
            //复制好友长度
            Array.ConstrainedCopy(friendLength, 0, packagebody, Offset, 4);
            Offset += 4;
            //复制好友
            Array.ConstrainedCopy(friend, 0, packagebody, Offset, friend.Length);
            Offset += friend.Length;
            //复制会话ID长度
            Array.ConstrainedCopy(SessionLength, 0, packagebody, Offset, 4);
            Offset += 4;
            //复制会话ID
            Array.ConstrainedCopy(Session, 0, packagebody, Offset, Session.Length);
            Offset += Session.Length;
            //得到协议
            byte[] protocol1 = ProtocolFactory.ProtocolFactory.GetProtocol("AddFriend",
                                                                          (int)Packet.Protocol.OUT_NetworkCommand.REDIRECT_ADD_FRIEND_PACKAGE,
                                                                          (int)Packet.Protocol.NetworkProtocol.Base,
                                                                          packagebody,
                                                                          ProtocolFactory.ProtocolFactory.ACK.No,
                                                                          ProtocolFactory.ProtocolFactory.ENCRYPT.No, "");
            NetControl.Send(protocol1, user.TalkPort, user.IpAddress);
        }

        /// <summary>
        ///     反馈给指定用户，标示当前该用户发送的添加好友结果已经收到
        /// </summary>
        /// <param name="inpackage"></param>
        public static void Retrun_PostAddFriendResult(PostAddFriendResultPacket inpackage)
        {
            byte[] Username = Encoding.Default.GetBytes(inpackage.GetUsername());
            byte[] UsernameLength = BitConverter.GetBytes(Username.Length);
            byte[] FriendName = Encoding.Default.GetBytes(inpackage.GetFriendName());
            byte[] FriendNameLength = BitConverter.GetBytes(FriendName.Length);
            byte[] Session = Encoding.Default.GetBytes(inpackage.GetSessionCode());
            byte[] SessionLength = BitConverter.GetBytes(Session.Length);
            byte[] packagebody = new byte[Username.Length + FriendName.Length + Session.Length + 12];
            int Offset = 0;
            Array.ConstrainedCopy(UsernameLength, 0, packagebody, Offset, 4);
            Offset += 4;
            Array.ConstrainedCopy(Username, 0, packagebody, Offset, Username.Length);
            Offset += Username.Length;
            Array.ConstrainedCopy(FriendNameLength, 0, packagebody, Offset, 4);
            Offset += 4;
            Array.ConstrainedCopy(FriendName, 0, packagebody, Offset, FriendName.Length);
            Offset += FriendName.Length;
            Array.ConstrainedCopy(SessionLength, 0, packagebody, Offset, 4);
            Offset += 4;
            Array.ConstrainedCopy(Session, 0, packagebody, Offset, Session.Length);
            Offset += Session.Length;
            //得到协议
            byte[] protocol1 = ProtocolFactory.ProtocolFactory.GetProtocol("Return_PostAddFriendResult",
                                                                          (int)Packet.Protocol.OUT_NetworkCommand.REPLY_RETURN_POST_ADD_FRIEND_PACKAGE,
                                                                          (int)Packet.Protocol.NetworkProtocol.Base,
                                                                          packagebody,
                                                                          ProtocolFactory.ProtocolFactory.ACK.No,
                                                                          ProtocolFactory.ProtocolFactory.ENCRYPT.No, "");
            NetControl.Send(protocol1, inpackage.GetTalkPort(), inpackage.GetUserIpAddress());
        }

        /// <summary>
        ///     广播指定用户 - 添加好友最终状态
        /// </summary>
        /// <param name="username">广播的用户名</param>
        /// <param name="FriendUsername">好有用户名</param>
        /// <param name="Boardcast_State">添加状态</param>
        /// <param name="ItemId">要添加的分组ID</param>
        /// <param name="FriendId">好友ID</param>
        public static void Boardcast_AddFriendResult(String username, String FriendUsername, BOARDCAST_ADDFIRNED Boardcast_State, String ItemId, String FriendId)
        {
            User.User user = Globle.userManager.GetUser(username);
            //用户在线
            if(user != null)
            {
                byte[] Username = Encoding.Default.GetBytes(username);
                byte[] UsernameLength = BitConverter.GetBytes(Username.Length);
                byte[] FriendName = Encoding.Default.GetBytes(FriendUsername);
                byte[] FriendNameLength = BitConverter.GetBytes(FriendName.Length);
                byte[] itemId = BitConverter.GetBytes(int.Parse(ItemId));
                byte[] friendId = BitConverter.GetBytes(int.Parse(FriendId));
                byte[] packagebody = new byte[Username.Length + FriendName.Length + 17];
                int Offset = 0;
                Array.ConstrainedCopy(UsernameLength, 0, packagebody, Offset, 4);
                Offset += 4;
                Array.ConstrainedCopy(Username, 0, packagebody, Offset, Username.Length);
                Offset += Username.Length;
                Array.ConstrainedCopy(FriendNameLength, 0, packagebody, Offset, 4);
                Offset += 4;
                Array.ConstrainedCopy(FriendName, 0, packagebody, Offset, FriendName.Length);
                Offset += FriendName.Length;
                Array.ConstrainedCopy(itemId, 0, packagebody, Offset, 4);
                Offset += 4;
                Array.ConstrainedCopy(friendId, 0, packagebody, Offset, 4);
                Offset += 4;
                byte[] AddState = null;
                if (Boardcast_State == BOARDCAST_ADDFIRNED.SUCESSFUL)
                {
                    AddState = BitConverter.GetBytes(true);
                }
                else
                {
                    AddState = BitConverter.GetBytes(false);
                }
                Array.ConstrainedCopy(AddState, 0, packagebody, Offset, 1);
                Offset += 1;
                //得到协议
                byte[] protocol1 = ProtocolFactory.ProtocolFactory.GetProtocol("BoardcastAddFriendResult",
                                                                              (int)Packet.Protocol.OUT_NetworkCommand.BOARDCAST_ADD_FRIEND,
                                                                              (int)Packet.Protocol.NetworkProtocol.Base,
                                                                              packagebody,
                                                                              ProtocolFactory.ProtocolFactory.ACK.No,
                                                                              ProtocolFactory.ProtocolFactory.ENCRYPT.No, "");
                NetControl.Send(protocol1, user.TalkPort, user.IpAddress);
            }
        }

        /// <summary>
        ///     广播添加用户状态
        /// </summary>
        public enum BOARDCAST_ADDFIRNED
        {
            /// <summary>
            ///     添加成功
            /// </summary>
            SUCESSFUL,
            /// <summary>
            ///     添加失败
            /// </summary>
            FAIL
        }
    }
}
